{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73ce8c5e",
   "metadata": {},
   "source": [
    "# IPython解释器\n",
    "\n",
    "通常不使用Python自带的解释器，而使用另一个更强大的解释器——IPython解释器。Anaconda自带IPython解释器。命令行输入IPython进入：\n",
    "```\n",
    "ipython\n",
    "```\n",
    "\n",
    "如果系统提示没有IPython可以通过pip命令安装：\n",
    "\n",
    "```shell\n",
    "pip install ipython\n",
    "```\n",
    "\n",
    "所有在Python解释器下可以运行的代码都可以在IPython解释器下运行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb89cf21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello, world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7f60560",
   "metadata": {},
   "source": [
    "赋值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "55b338f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feba21bc",
   "metadata": {},
   "source": [
    "输入变量名查看值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5c533e88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1eeabe52",
   "metadata": {},
   "source": [
    "变量可以任意被覆盖："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4eb5a259",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"test\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "78fa3997",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953e4a4b",
   "metadata": {},
   "source": [
    "## 魔术命令\n",
    "\n",
    "相对于Python解释器，IPython解释器额外提供了很多以百分号%开头的魔术命令，这些命令很像Mac/Linux系统下的命令行命令（事实上有些是一样的）。\n",
    "\n",
    "查看所有的模式命令："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "14143fa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": {
       "cell": {
        "!": "OSMagics",
        "HTML": "Other",
        "SVG": "Other",
        "bash": "Other",
        "capture": "ExecutionMagics",
        "debug": "ExecutionMagics",
        "file": "Other",
        "html": "DisplayMagics",
        "javascript": "DisplayMagics",
        "js": "DisplayMagics",
        "latex": "DisplayMagics",
        "markdown": "DisplayMagics",
        "perl": "Other",
        "prun": "ExecutionMagics",
        "pypy": "Other",
        "python": "Other",
        "python2": "Other",
        "python3": "Other",
        "ruby": "Other",
        "script": "ScriptMagics",
        "sh": "Other",
        "svg": "DisplayMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "writefile": "OSMagics"
       },
       "line": {
        "alias": "OSMagics",
        "alias_magic": "BasicMagics",
        "autoawait": "AsyncMagics",
        "autocall": "AutoMagics",
        "automagic": "AutoMagics",
        "autosave": "KernelMagics",
        "bookmark": "OSMagics",
        "cat": "Other",
        "cd": "OSMagics",
        "clear": "KernelMagics",
        "colors": "BasicMagics",
        "conda": "PackagingMagics",
        "config": "ConfigMagics",
        "connect_info": "KernelMagics",
        "cp": "Other",
        "debug": "ExecutionMagics",
        "dhist": "OSMagics",
        "dirs": "OSMagics",
        "doctest_mode": "BasicMagics",
        "ed": "Other",
        "edit": "KernelMagics",
        "env": "OSMagics",
        "gui": "BasicMagics",
        "hist": "Other",
        "history": "HistoryMagics",
        "killbgscripts": "ScriptMagics",
        "ldir": "Other",
        "less": "KernelMagics",
        "lf": "Other",
        "lk": "Other",
        "ll": "Other",
        "load": "CodeMagics",
        "load_ext": "ExtensionMagics",
        "loadpy": "CodeMagics",
        "logoff": "LoggingMagics",
        "logon": "LoggingMagics",
        "logstart": "LoggingMagics",
        "logstate": "LoggingMagics",
        "logstop": "LoggingMagics",
        "ls": "Other",
        "lsmagic": "BasicMagics",
        "lx": "Other",
        "macro": "ExecutionMagics",
        "magic": "BasicMagics",
        "man": "KernelMagics",
        "matplotlib": "PylabMagics",
        "mkdir": "Other",
        "more": "KernelMagics",
        "mv": "Other",
        "notebook": "BasicMagics",
        "page": "BasicMagics",
        "pastebin": "CodeMagics",
        "pdb": "ExecutionMagics",
        "pdef": "NamespaceMagics",
        "pdoc": "NamespaceMagics",
        "pfile": "NamespaceMagics",
        "pinfo": "NamespaceMagics",
        "pinfo2": "NamespaceMagics",
        "pip": "PackagingMagics",
        "popd": "OSMagics",
        "pprint": "BasicMagics",
        "precision": "BasicMagics",
        "prun": "ExecutionMagics",
        "psearch": "NamespaceMagics",
        "psource": "NamespaceMagics",
        "pushd": "OSMagics",
        "pwd": "OSMagics",
        "pycat": "OSMagics",
        "pylab": "PylabMagics",
        "qtconsole": "KernelMagics",
        "quickref": "BasicMagics",
        "recall": "HistoryMagics",
        "rehashx": "OSMagics",
        "reload_ext": "ExtensionMagics",
        "rep": "Other",
        "rerun": "HistoryMagics",
        "reset": "NamespaceMagics",
        "reset_selective": "NamespaceMagics",
        "rm": "Other",
        "rmdir": "Other",
        "run": "ExecutionMagics",
        "save": "CodeMagics",
        "sc": "OSMagics",
        "set_env": "OSMagics",
        "store": "StoreMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "tb": "ExecutionMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "unalias": "OSMagics",
        "unload_ext": "ExtensionMagics",
        "who": "NamespaceMagics",
        "who_ls": "NamespaceMagics",
        "whos": "NamespaceMagics",
        "xdel": "NamespaceMagics",
        "xmode": "BasicMagics"
       }
      },
      "text/plain": [
       "Available line magics:\n",
       "%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %conda  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode\n",
       "\n",
       "Available cell magics:\n",
       "%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile\n",
       "\n",
       "Automagic is ON, % prefix IS NOT needed for line magics."
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%lsmagic"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59dbe922",
   "metadata": {},
   "source": [
    "其中：\n",
    "- `line magic`以一个百分号开头，作用与一行。\n",
    "- `cell magic`以两个百分号开头，作用于整个cell。\n",
    "- 最后一行`Automagic is ON, % prefix IS NOT needed for line magics.`说明在此时即使不加上%也可以使用`line magic`命令。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ab70b56",
   "metadata": {},
   "source": [
    "使用 whos 查看当前的变量空间："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ad1d5f00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable   Type    Data/Info\n",
      "----------------------------\n",
      "a          str     test\n"
     ]
    }
   ],
   "source": [
    "%whos"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbce95c7",
   "metadata": {},
   "source": [
    "使用 reset 重置当前变量空间："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "436c9d4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%reset -f"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e86dddb",
   "metadata": {},
   "source": [
    "再使用 whos 查看当前的变量空间："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8888666b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Interactive namespace is empty.\n"
     ]
    }
   ],
   "source": [
    "%whos"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0cd9304",
   "metadata": {},
   "source": [
    "使用 pwd 查看当前工作文件夹："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "63607822",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/lijin/Documents/Projects/notes-python3/ch01-Python介绍'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a14cbd65",
   "metadata": {},
   "source": [
    "使用 mkdir 产生新文件夹："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "758feb75",
   "metadata": {},
   "outputs": [],
   "source": [
    "%mkdir demo_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33c54e74",
   "metadata": {},
   "source": [
    "使用 cd 改变工作文件夹："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2602f659",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/lijin/Documents/Projects/notes-python3/ch01-Python介绍/demo_test\n"
     ]
    }
   ],
   "source": [
    "%cd demo_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ba71af",
   "metadata": {},
   "source": [
    "使用 writefile 将cell中的内容写入文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6695166e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing hello_world.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile hello_world.py\n",
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33c4e93e",
   "metadata": {},
   "source": [
    "使用 ls 查看当前工作文件夹的文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "350daadb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello_world.py\r\n"
     ]
    }
   ],
   "source": [
    "%ls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "558f25b8",
   "metadata": {},
   "source": [
    "返回上一层文件夹："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "024bed89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/lijin/Documents/Projects/notes-python3/ch01-Python介绍\n"
     ]
    }
   ],
   "source": [
    "%cd .."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11638d97",
   "metadata": {},
   "source": [
    "使用 rm 删除文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8ae0020d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%rm demo_test/hello_world.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03bf5a5d",
   "metadata": {},
   "source": [
    "使用 rmdir 删除文件夹："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c411cad9",
   "metadata": {},
   "outputs": [],
   "source": [
    "%rmdir demo_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8145246",
   "metadata": {},
   "source": [
    "使用 hist 查看历史命令："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "20258093",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "print(\"hello, world\")\n",
      "a = 1\n",
      "a\n",
      "a = \"test\"\n",
      "a\n",
      "%lsmagic\n",
      "%whos\n",
      "%reset -f\n",
      "%whos\n",
      "%pwd\n",
      "%mkdir demo_test\n",
      "%cd demo_test\n",
      "%%writefile hello_world.py\n",
      "print(\"hello world\")\n",
      "%ls\n",
      "%cd ..\n",
      "%rm demo_test/hello_world.py\n",
      "%rmdir demo_test\n",
      "%hist\n"
     ]
    }
   ],
   "source": [
    "%hist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f18f29d3",
   "metadata": {},
   "source": [
    "## 其他用法\n",
    "\n",
    "使用 ? 查看函数的帮助："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c32871a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fe83612",
   "metadata": {},
   "source": [
    "使用 ?? 查看函数帮助和函数源代码（如果是用python实现的）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1de72933",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: <object object at 0x10796c470>\n",
      "%pylab is deprecated, use %matplotlib inline and import the required libraries.\n",
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "# 导入numpy和matplotlib两个包\n",
    "%pylab\n",
    "# 查看其中sort函数的帮助\n",
    "sort??"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8b25de5",
   "metadata": {},
   "source": [
    "使用 _ 使用上个cell的输出结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5bdb507e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 12\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4748138b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_ + 13"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0a55e05",
   "metadata": {},
   "source": [
    "可以使用 ! 来执行一些系统命令。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3c201d4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\r\n"
     ]
    }
   ],
   "source": [
    "!python -c 'print(\"hello world\")'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a35db19",
   "metadata": {},
   "source": [
    "输入代码不合法时，会给出错误提示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "966742ac",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [24]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "1 + 'hello'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "896ec9a6",
   "metadata": {},
   "source": [
    "虽然输入错误，但解释器不会停止运行，还可以继续输入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "aa957b85",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "314e014a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
